package com.bawei.mall.user.service.Impl;

import com.alibaba.fastjson2.JSONObject;
import com.bawei.common.core.exception.ServiceException;
import com.bawei.mall.product.domain.model.MallUserFollow;
import com.bawei.mall.product.domain.model.UserFollowList;
import com.bawei.mall.user.domain.request.UserInformati;
import com.bawei.mall.user.mapper.MallUserFollowMapper;
import com.bawei.mall.user.service.MallUserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：张余庆
 * @Package：com.bawei.mall.user.service.Impl
 * @Project：mall_cloud
 * @name：MallUserFollowServiceImpl
 * @Date：2024/7/4 19:48
 */
@Service
public class MallUserFollowServiceImpl implements MallUserFollowService {

    @Autowired
    private MallUserFollowMapper mapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 功能：查询用户关注表
     * @return
     */
    @Override
    public UserFollowList list(Integer pageNum, Integer pageSize) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        UserFollowList list = new UserFollowList();
        List<MallUserFollow> mallUserFollow = mapper.list(userId,pageNum,pageSize);
        list.setList(mallUserFollow);
        return list;
    }

    /**
     * 功能：添加用户关注表
     * @param mallUserFollow
     */
    @Override
    public void add(MallUserFollow mallUserFollow) {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        mallUserFollow.setUserId(userId);
        Integer i = mapper.add(mallUserFollow);
        if(i<=0){
            throw new RuntimeException("添加收藏说失败");
        }
    }

    /**
     * 功能：清空用户关注
     */
    @Override
    public void clear() {
        String token = redisTemplate.opsForValue().get("aa");
        if (token == null || token.isEmpty()) {
            throw new ServiceException("Token为空");
        }
        String cusUserJSONString = redisTemplate.opsForValue().get(token);
        if (cusUserJSONString == null) {
            throw new ServiceException("未找到用户信息");
        }
        UserInformati userInformati = JSONObject.parseObject(cusUserJSONString, UserInformati.class);
        Long userId = userInformati.getUserId();
        Integer i = mapper.clear(userId);
        if(i<=0){
            throw new RuntimeException("清空失败");
        }
    }
}
